<template>
  <div class="app-container">
    <div class="panel">
      <el-form :model="formInline" inline class="search-form">
        <el-form-item label="用户" size="small">
          <el-input
            v-model="formInline.user"
            clearable
            placeholder="姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色权限" size="small">
          <el-select v-model="roleValue" clearable placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="small">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="search"
            @click="initGetUsers"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item size="small">
          <el-button
            type="success"
            icon="el-icon-plus"
            @click="openAddUserDialog"
            class="search"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="source">
      <el-table
        :data="list"
        v-loading.body="listLoading"
        element-loading-text="Loading"
        highlight-current-row
        @selection-change="handleSelectionChange"
        size="small"
        style="width: 100%"
        max-height="450"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="姓名">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="手机号">
          <template slot-scope="scope">
            <span>{{ scope.row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="角色权限">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.roles" :key="index">
              <el-tag type="gray">{{ item }}</el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="最后登录时间" width="220">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span>{{ scope.row.last_login_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最后登录IP" width="220">
          <template slot-scope="scope">
            <span>{{ scope.row.last_ip }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :disabled="scope.row.is_keeper === 1"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-popover
              placement="top"
              width="160"
              trigger="click"
              v-model="scope.row.delPopover"
            >
              <p>确定删除此用户吗？</p>
              <div>
                <el-button
                  size="mini"
                  type="primary"
                  @click="scope.row.delPopover = false"
                  >取消</el-button
                >
                <el-button
                  size="mini"
                  type="warning"
                  @click="handleDelete(scope.$index, scope.row)"
                  >确定</el-button
                >
              </div>
              <el-button
                slot="reference"
                type="danger"
                size="mini"
                :disabled="scope.row.is_keeper === 1"
                >删除</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[20, 50, 100]"
          :page-size="pageSize"
          :current-page="page"
          layout="sizes, prev, pager, next"
          :total="totalList"
        ></el-pagination>
      </div>
    </div>
    <AddUser
      :addUserDialog.sync="addUserDialog"
      :list.sync="list"
      :auths="roles"
    ></AddUser>
    <EditUser
      :editUserDialog.sync="editUserDialog"
      :list.sync="list"
      :info="editData"
      :auths="roles"
    ></EditUser>
  </div>
</template>

<script>
import { getUsers, getRoles, deleteUser } from '@/api/group'
import { AddUser, EditUser } from '@/components'
export default {
  data() {
    return {
      list: [],
      totalList: 1,
      page: 1,
      pageSize: 20,
      listLoading: true,
      visibleDelete: false, // 删除按钮提示
      multipleSelection: [], // 表单多选
      addUserDialog: false, // 新增用户
      editUserDialog: false, // 编辑用户
      formInline: {
        user: ''
      },
      roles: [],
      roleValue: '',
      editData: {}
    }
  },
  components: {
    AddUser,
    EditUser
  },
  filters: {},
  created() {
    this.fetchData()
    this.initGetRoles()
  },
  methods: {
    fetchData() {
      const _this = this
      _this.listLoading = true
      const datas = {
        nickname: this.formInline.user,
        group_id: this.roleValue,
        page: this.page,
        limit: this.pageSize
      }
      getUsers(datas)
        .then((res) => {
          this.list = res.data.data
          this.totalList = res.data.total
          this.listLoading = false
        })
        .catch(() => {
          _this.listLoading = false
        })
    },
    initGetUsers() {
      this.handleCurrentChange(1)
    },
    initGetRoles() {
      const _this = this
      getRoles()
        .then((res) => {
          if (res.code === 1) {
            // 权限组添加
            const list = res.data
            for (const i in list) {
              _this.roles.push(list[i])
            }
          }
        })
        .catch(() => {})
    },
    handleEdit(index, row) {
      this.$set(this.editData, 'index', index)
      this.$set(this.editData, 'row', row)
      this.editUserDialog = true
    },
    handleDelete(index, row) {
      const _this = this
      deleteUser(row.uid)
        .then((res) => {
          if (res.code === 1) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            _this.list.splice(index, 1)
            row.delPopover = false
          }
        })
        .catch(() => {
          // console.log('失败了')
        })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page = val
      this.fetchData()
    },
    openAddUserDialog() {
      this.addUserDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.panel {
  padding: 24px;
  border: 1px solid #ebebeb;
  border-radius: 3px;
  transition: 0.2s;
  background-color: #f9fafc;
}
.source {
  padding: 24px;
  border: 1px solid #ebebeb;
  border-radius: 3px;
  transition: 0.2s;
  margin-top: 24px;
}
.table {
  width: 100%;
}
// 滚动条的宽度
/deep/ .el-table__body-wrapper::-webkit-scrollbar {
  width: 6px; // 横向滚动条
  height: 6px; // 纵向滚动条
}
// 滚动条的滑块
/deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 3px;
}
.search-form {
  width: 100%;
  .el-input {
    width: 80%;
  }
  .el-select {
    width: 80%;
    .el-option {
      width: 100%;
    }
  }
  .search {
    width: 100%;
  }
  .line {
    text-align: center;
  }
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
